package geor.view;

import java.util.Calendar;
import java.util.GregorianCalendar;

import geor.app.R;
import geor.model.CompraHelper;
import lib.util.DateTimeUtils;
import lib.view.View;
import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.util.Log;
import android.webkit.WebView;

public class GraficoActivity extends Activity implements View {

	public static final String TIPO_GRAFICO = "geor.app.grafico";
	private int tipoGrafico;
	private WebView webView = null;
	private CompraHelper helper = null;
	private Cursor model = null;
	private Calendar calendar = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.grafico);

		helper = new CompraHelper(this);
		calendar = new GregorianCalendar();

		load();
	}

	@Override
	public void load() {
		webView = (WebView) findViewById(R.id.wv_grafico);
		webView.getSettings().setJavaScriptEnabled(true);

		tipoGrafico = Integer.valueOf(getIntent().getStringExtra(TIPO_GRAFICO));
		switch (tipoGrafico) {
		case 0:
			resumoComrpasMes();

			break;
		case 1:
			comprasMesAtual();
			break;
		}
	}

	@Override
	public void set() {
	}

	public void resumoComrpasMes() {
		model = helper.resumoComprasMes();
		String h = "ano=";
		String v = "&soma=[";

		// iterar pelo cursor para obter os valores
		while (model.moveToNext()) {
			if (model.isFirst()) {
				h = h + model.getString(0);
				v = v + model.getDouble(2);

			} else {
				h = h + "-" + model.getString(0);
				v = v + "," + model.getDouble(2);
			}
			// t=t+model.getString(1);
		}

		v = v + "]";

		webView.loadUrl("file:///android_asset/graf_compras_meses_ano.html?"
				+ h + v);
	}

	public void comprasMesAtual() {
		String date = DateTimeUtils.formatDate(calendar.getTime(),
				DateTimeUtils.DEF_MES_ANO);
		model = helper.comprasMesAtual(CompraHelper.WHERE_MES_ANO,
				new String[] { date });
		String h = "contas_do_mes=";
		String v = "&c_pagas=";

		// iterar pelo cursor para obter os valores
		model.moveToFirst();

		h = h + model.getString(0);
		v = v + model.getString(1);

		// h=h+150;
		// v=v+200;

		// t=t+model.getString(1);

		Log.i("teste", h + v);
		webView.loadUrl("file:///android_asset/graf_resumo_compras_mes_atual.html?"
				+ h + v);
	}
}
